package com.estore.model;
import java.util.Objects;
import java.util.Set;
import java.util.HashSet;

/**
 * 商品实体类
 * 实现Comparable接口，默认按商品ID排序
 */
public class Product implements Comparable<Product> {

    private String id;      // 商品ID
    private String name;    // 商品名称
    private double price;   // 价格
    private int stock;      // 库存
    private Set<String> tags; // 商品标签

    /**
     * 构造函数 - 带标签
     */
    public Product(String id, String name, double price, int stock, Set<String> tags) {
        this.id = Objects.requireNonNull(id, "商品ID不能为null");
        this.name = Objects.requireNonNull(name, "商品名称不能为null");
        setPrice(price);
        setStock(stock);
        this.tags = tags != null ? new HashSet<>(tags) : new HashSet<>();
    }

    /**
     * 构造函数 - 不带标签（初始化为空HashSet）
     */
    public Product(String id, String name, double price, int stock) {
        this(id, name, price, stock, new HashSet<>());
    }

    // Getters and Setters
    public String getId() { return id; }

    public void setId(String id) {
        this.id = Objects.requireNonNull(id, "商品ID不能为null");
    }

    public String getName() { return name; }

    public void setName(String name) {
        this.name = Objects.requireNonNull(name, "商品名称不能为null");
    }

    public double getPrice() { return price; }

    public void setPrice(double price) {
        if (price < 0) {
            throw new IllegalArgumentException("价格不能为负数");
        }
        this.price = price;
    }

    public int getStock() { return stock; }

    public void setStock(int stock) {
        if (stock < 0) {
            throw new IllegalArgumentException("库存不能为负数");
        }
        this.stock = stock;
    }

    public Set<String> getTags() {
        return new HashSet<>(tags); // 返回副本以保护内部数据
    }

    public void setTags(Set<String> tags) {
        this.tags = tags != null ? new HashSet<>(tags) : new HashSet<>();
    }

    /**
     * 添加单个标签
     */
    public void addTag(String tag) {
        if (tag != null && !tag.trim().isEmpty()) {
            this.tags.add(tag.trim());
        }
    }

    /**
     * 移除单个标签
     */
    public boolean removeTag(String tag) {
        return this.tags.remove(tag);
    }

    /**
     * 检查是否包含某个标签
     */
    public boolean hasTag(String tag) {
        return this.tags.contains(tag);
    }

    /**
     * 获取标签数量
     */
    public int getTagCount() {
        return this.tags.size();
    }

    /**
     * 清空所有标签
     */
    public void clearTags() {
        this.tags.clear();
    }

    /**
     * 减少库存
     * @param quantity 减少的数量
     * @return 是否成功减少
     */
    public boolean decreaseStock(int quantity) {
        if (quantity <= 0 || quantity > stock) {
            return false;
        }
        this.stock -= quantity;
        return true;
    }

    /**
     * 增加库存
     * @param quantity 增加的数量
     */
    public void increaseStock(int quantity) {
        if (quantity <= 0) {
            throw new IllegalArgumentException("增加数量必须为正数");
        }
        this.stock += quantity;
    }

    @Override
    public String toString() {
        String baseInfo = String.format("ID: %-5s | 名称: %-20s | 价格: ¥%-8.2f | 库存: %d",
                id, name, price, stock);

        // 如果有标签，则在末尾显示标签信息
        if (tags != null && !tags.isEmpty()) {
            return baseInfo + " | 标签: " + tags;
        } else {
            return baseInfo;
        }
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Product product = (Product) o;
        return Objects.equals(id, product.id);
    }

    @Override
    public int hashCode() {
        return Objects.hash(id);
    }

    @Override
    public int compareTo(Product other) {
        return this.id.compareTo(other.id);
    }

    /**
     * 创建商品副本
     */
    public Product copy() {
        return new Product(this.id, this.name, this.price, this.stock, this.tags);
    }
}